﻿using UnityEngine;
using System.Collections;
namespace Room
{
    [AddComponentMenu("Room/RoomCharactor")]
    public class RoomCharactor : MonoBehaviour


    {
        public CharactorType type = CharactorType.None;
        public RoomCharactorManager charactorManager;
        public string extraInfo;
        public bool isUiInView = true;
        private float lastActiveTime;
        public float lastUpdateTime;
        
        private bool inView;
        public bool InView
        {
            set
            {
                if(value==true && inView==false)
                {
                    Check();
                }
                inView = value;
            }
        }

        void Check()
        {
            if (RoomManager.curSceneType == SceneType.FIELD)
            {
                var room = RoomManager.currentRoom as RoomField;
                var netManager = room.netManager as RoomNetField;
                if (type == CharactorType.FieldBoss)
                {
                    netManager.SendCheckPveBoss(name);
                }
                else if (type == CharactorType.ServerMonster)
                {
                    netManager.SendCheckPveMonster(name);
                }
                else if (type == CharactorType.Fish ||
                    type == CharactorType.Mine)
                {
                    var pos = name.Split(new char[1] { '@' });
                    netManager.SendResourceCheck(pos[1]);
                }
            }
        }
       

        public RCAIBase ai;
        public RCLODBase lod;
        public RCMoveBase move;
        public RCUIBase ui;

#if UNITY_EDITOR
        public int Camp = -999;
        public UnionActorState actorState;
        public UnionGuardState guardState;
        public UnionChariotState chariotState;
        public UnionMonsterState monsterState;
        public UnionChariotFactoryState factoryState;
        

        void Update()
        {
            actorState = UnionActorState.None;
            guardState = UnionGuardState.None;
            chariotState = UnionChariotState.None;
            monsterState = UnionMonsterState.None;
            factoryState = UnionChariotFactoryState.None;


            var __ai = ai as RCAIUnionFightBase;
            if(__ai!=null)
            {
                this.Camp = __ai.Camp;
            }



            var _ai = ai as RCAIUnionActor;
            if(_ai!=null)
            {
                actorState = _ai.CurrState;
            }
            var _ai0 = ai as RCAIGuard;
            if(_ai0 != null)
            {
                guardState = _ai0.CurrState;
            }

            var _ai1 = ai as RCAIChariot;
            if(_ai1!=null)
            {
                chariotState = _ai1.CurrState;
            }

            var _ai2 = ai as RCAIUnionMonster;
            if(_ai2!=null)
            {
                monsterState = _ai2.CurrState;
            }

            var _ai3 = ai as RCAIChariotFactory;
            if(_ai3 != null)
            {
                factoryState = _ai3.CurrState;
            }
            
            
        }
#endif

        public float LastActiveTime
        {
            get
            {
                if (type == CharactorType.OtherPlayer)
                {
                    return lastActiveTime;
                }
                return Time.unscaledTime;
            }
        }

        private bool running = false;

        public bool Running
        {
            get
            {
                return running;
            }
        }

        public void Reset(AIType aiType, LODType lodType, MoveType moveType, UIType uiType)
        {
            if (ai == null || ai.type != aiType)
            {
                switch (aiType)
                {
                    case AIType.None:
                        ai = new RCAIBase(this);
                        break;
                    case AIType.Self:
                        ai = new RCAISelf(this);
                        break;
                    case AIType.Monster:
                        ai = new RCAIMonster(this);
                        break;
                    case AIType.OtherPlayer:
                        ai = new RCAIOtherPlayer(this);
                        break;
                    case AIType.NormalNPC:
                        ai = new RCAINormalNPC(this);
                        break;
                    case AIType.Area:
                        ai = new RCAIArea(this);
                        break;
                    case AIType.Box:
                        ai = new RCAIBox(this);
                        break;
                    case AIType.AirWall:
                        ai = new RCAIAirWall(this);
                        break;
                    case AIType.Fish:
                        ai = new RCAIFish(this);
                        break;
                    case AIType.Mine:
                        ai = new RCAIMine(this);
                        break;
                    case AIType.SafeArea:
                        ai = new RCAISafeArea(this);
                        break;
                    case AIType.Prison:
                        ai = new RCAIPrison(this);
                        break;
                    case AIType.ServerMonster:
                        ai = new RCAIServerMonster(this);
                        break;
                    case AIType.FieldBoss:
                        ai = new RCAIFieldBoss(this);
                        break;
                    case AIType.PatrolNPC:
                        ai = new RCAIPatrolNPC(this);
                        break;
                    case AIType.UnionTower:
                        ai = new RCAITower(this);
                        break;
                    case AIType.UnionChariot:
                        ai = new RCAIChariot(this);
                        break;
                    case AIType.UnionChariotFactory:
                        ai = new RCAIChariotFactory(this);
                        break;
                    case AIType.UnionGate:
                        ai = new RCAIGate(this);
                        break;
                    case AIType.UnionGuard:
                        ai = new RCAIGuard(this);
                        break;
                    case AIType.UnionMonster:
                        ai = new RCAIUnionMonster(this);
                        break;
                    case AIType.UnionTree:
                        ai = new RCAITree(this);
                        break;
                    case AIType.UnionMine:
                        ai = new RCAIUnionMine(this);
                        break;
                    case AIType.UnionRevivePoint:
                        ai = new RCAIRevivePoint(this);
                        break;
                    case AIType.UnionSelf:
                        ai = new RCAIUnionSelf(this);
                        break;
                    case AIType.UnionOther:
                        ai = new RCAIUnionOther(this);
                        break;
                    case AIType.UnionBaseCamp:
                        ai = new RCAIBaseCamp(this);
                        break;
                    case AIType.UnionTotem:
                        ai = new RCAIUnionFightTotem(this);
                        break;
					case AIType.UnionTransfer:
						ai = new RCAIUnionTransfer(this);
						break;
                    case AIType.Pet:
                        ai = new RCAIPet(this);
                        break;
                }
            }

            if (lod == null || lod.type != lodType)
            {
                switch (lodType)
                {
                    case LODType.None:
                        lod = new RCLODBase(this);
                        break;
                    case LODType.Player:
                        lod = new RCLODPlayer(this);
                        break;
                    case LODType.Box:
                        lod = new RCLODBox(this);
                        break;
                    case LODType.AirWall:
                        lod = new RCLODAirWall(this);
                        break;
                    case LODType.Mine:
                        lod = new RCLODMine(this);
                        break;
                    case LODType.Fish:
                        lod = new RCLODFish(this);
                        break;
                    case LODType.Stronghold:
                        lod = new RCLODRaidHold(this);
                        break;
                    case LODType.UnionChariot:
                        lod = new RCLODUnionChariot(this);
                        break;
                    case LODType.UnionGuard:
                        lod = new RCLODUnionGuard(this);
                        break;
                    case LODType.UnionMine:
                        lod = new RCLODUnionMine(this);
                        break;
                    case LODType.UnionNpc:
                        lod = new RCLODUnionNpc(this);
                        break;
                    case LODType.UnionRevivePoint:
                        lod = new RCLODUnionRevivePoint(this);
                        break;
                    case LODType.Pet:
                        lod = new RCLODPet(this);
                        break;
                }
            }

            if (move == null || move.type != moveType)
            {
                switch (moveType)
                {
                    case MoveType.None:
                        move = new RCMoveBase(this);
                        break;
                    case MoveType.Simple:
                        move = new RCMoveSimple(this);
                        break;
                    case MoveType.Navigation:
                        move = new RCMoveNavigation(this);
                        break;
                    case MoveType.Monster:
                        move = new RCMoveMonster(this);
                        break;
                    case MoveType.SamplePosition:
                        move = new RCMoveSamplePosition(this);
                        break;
                    case MoveType.Fixed:
                        move = new RCMoveFixed(this);
                        break;
                    case MoveType.Chariot:
                        move = new RCMoveChariot(this);
                        break;
                   }
            }
            if (ui == null || ui.type != uiType)
            {
                switch (uiType)
                {
                    case UIType.None:
                        ui = new RCUIBase(this);
                        break;
                    case UIType.Player:
                        ui = new RCUIPlayer(this);
                        break;
                    case UIType.NormalNPC:
                        ui = new RCUINormalNPC(this);
                        break;
                    case UIType.StoryNPC:
                        ui = new RCUIStoryNPC(this);
                        break;
                    case UIType.Box:
                        ui = new RCUIBox(this);
                        break;
                    case UIType.Mine:
                        ui = new RCUIMine(this);
                        break;
                    case UIType.Fish:
                        ui = new RCUIFish(this);
                        break;
                    case UIType.MineGuide:
                        ui = new RCUIMineGuide(this);
                        break;
                    case UIType.FishGuide:
                        ui = new RCUIFishGuide(this);
                        break;
                    case UIType.Stronghold:
                        ui = new RCUIRaidHold(this);
                        break;
                    case UIType.ServerMonster:
                        ui = new RCUIServerMonster(this);
                        break;
                    case UIType.FieldBoss:
                        ui = new RCUIFieldBoss(this);
                        break;
                    case UIType.UnionNPC:
                        ui = new RCUIUnionNPC(this);
                        break;
                    case UIType.UnionChariot:
                        ui = new RCUIUnionChariot(this);
                        break;
                    case UIType.UnionRevivePoint:
                        ui = new RCUIUnionRevivePoint(this);
                        break;
                    case UIType.UnionActor:
                        ui = new RCUIUnionActor(this);
                        break;
                    case UIType.UnionMine:
                        ui = new RCUIUnionMine(this);
                        break;
                    case UIType.UnionGuard:
                        ui = new RCUIUnionGuard(this);
                        break;
                    case UIType.UnionFightNpc:
                        ui = new RCUIUnionFightNpc(this);
                        break;
                    case UIType.UnionGate:
                        ui = new RCUIUnionGate(this);
                        break;
                    case UIType.UnionMonster:
                        ui = new RCUIUnionFightMonster(this);
                        break;
                    case UIType.UnionTotem:
                        ui = new RCUIUnionFightTotem(this);
                        break;
                }
            }
            lastActiveTime = Time.unscaledTime;
            lastUpdateTime = Time.unscaledTime;
        }

        public void Tick()
        {
            ai.Tick();
            move.Tick();
            lod.Tick();
            ui.Tick();
        }
        public void LateTick()
        {
            if (ui == null)
            {
                Debug.LogError(name + "ui is null");
            }
            ui.LateTick();
        }
        public void DoStart()
        {
            ai.DoStart();
            lod.DoStart();
            ui.DoStart();
            move.DoStart();

        }
        public void DoStop()
        {
            ai.DoStop();
            lod.DoStop();
            ui.DoStop();
            move.DoStop();

        }
        public void DoResume()
        {
            if (!running)
            {
                lod.DoResume();
                move.DoResume();
                ui.DoResume();
                ai.DoResume();
                running = true;
            }
        }
        public void DoPause()
        {
            if (running)
            {
                ai.DoPause();
                lod.DoPause();
                ui.DoPause();
                move.DoPause();
                running = false;
            }
        }

        public void ResetPosition()
        {
            move.ResetPosition();
        }
        void OnDestroy()
        {
            //if(running)
            //{
            //    DoStop();
            //}
        }

        public void SetSpeed(float speed)
        {
            move.SetSpeed(speed);
        }



        #region lod

        public void SetLodLevel(int level)
        {
            lod.SetLevel(level);
            if(level == 0)
            {
                isUiInView = false;
            }
            else
            {
                isUiInView = true;
            }
        }

        public int GetLodLevel()
        {
            return lod.GetLevel();
        }

        public void PlayAnim(string animName, WrapMode wrapMode = WrapMode.Loop)
        {
            lod.PlayAnim(animName, wrapMode);
        }

        public float GetHeight()
        {
            return lod.GetHeight();
        }

        #endregion

        #region move

        public void MoveTo(Vector3 to)
        {
            move.MoveTo(to);
            lastActiveTime = Time.unscaledTime;
            lastUpdateTime = Time.unscaledTime;
            //Debug.Log(name + ":MoveTo:" + to);
        }

        public void StopTo(Vector3 to)
        {
            move.StopTo(to);
            lastActiveTime = Time.unscaledTime;
            lastUpdateTime = Time.unscaledTime;
            //Debug.Log(name + ":StopTo:" + to);
        }

        public void LerpPosition(Vector3 to)
        {
            move.LerpPosition(to);
            lastActiveTime = Time.unscaledTime;
            lastUpdateTime = Time.unscaledTime;
        }

        public void StopMove()
        {
            move.StopMove();
        }

        public bool IsMoving()
        {
            return move.IsMoving();
        }

        #endregion

        public void SetInView(bool inView)
        {
            lod.SetInView(inView);
            ui.SetInView(inView);

            InView = inView;
        }

        public bool GetInView()
        {
            return lod.GetInView();
        }

        public void SetKillNum(int killNum)
        {
            ai.SetKillNum(killNum);
        }

        public int GetKillNum()
        {
            return ai.GetKillNum();
        }
        public void ShowBubble(string text)
        {
            ui.ShowBubble(text);
        }

        public void Click()
        {
            ui.Click();
        }

        public void StopAnimation()
        {
            lod.StopAnim();
        }

        public void RestoreAnimation()
        {
            lod.ResumeAnim();
        }

        public void RotateTo(float angle)
        {
            move.RotateTo(angle);
        }
    }
}
